package com.liusand.service.impl;

import cn.hutool.core.util.IdUtil;
import com.liusand.common.UserThreadLocalUtils;
import com.liusand.dto.AddressBookDto;
import com.liusand.dto.IdDto;
import com.liusand.exception.AddressBookException;
import com.liusand.mapper.AddressBookMapper;
import com.liusand.pojo.AddressBook;
import com.liusand.service.AddressBookService;
import com.liusand.vo.AddressBookVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;



/**
 * @author :liusha
 * packageName :com.liusand.service.impl
 * fileName :AddressBookServiceImpl
 * creationTime :2022/12/5 16:56
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    @Override
    public List<AddressBookVo> list() {
        List<AddressBookVo> list = addressBookMapper.list();
        return list;
    }

    @Override
    public AddressBookVo findAddressBookById(String id) {
        if(id == null){
            return null;
        }
        AddressBookVo addressBookVo = addressBookMapper.selectAddressBooktById(id);
        return addressBookVo;
    }

    @Override
    public AddressBookVo findDefaultAddressBook() {
        AddressBookVo addressBookVo = addressBookMapper.selectDefaultAddressBook();
        return addressBookVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modifyDefaultAddressBook(IdDto idDto) {
        if (idDto == null || !StringUtils.hasText(idDto.getId())) {
            return false;
        }
        AddressBookVo addressBookVo = addressBookMapper.selectDefaultAddressBook();
        int result = 0;
        if(addressBookVo != null){
            result = addressBookMapper.updateDefaultAddressBook(addressBookVo.getId(), 0);
            if(result != 1){
                throw new AddressBookException("修改默认地址失败");
            }
        }

        result = addressBookMapper.updateDefaultAddressBook(idDto.getId(), 1);
        if(result != 1){
            throw new AddressBookException("修改默认地址失败");
        }
        return true;
    }

    @Override
    public boolean addAddressBook(AddressBookDto addressBookDto) {
        if(addressBookDto == null){
            return false;
        }
        AddressBook addressBook = new AddressBook();
        BeanUtils.copyProperties(addressBookDto, addressBook);
        addressBook.setId(IdUtil.getSnowflakeNextIdStr());
        addressBook.setUserId(UserThreadLocalUtils.getId());
        int insertResult = addressBookMapper.insertAddressBookt(addressBook);
        return insertResult == 1;
    }

    @Override
    public boolean modifyAddressBook(AddressBookDto addressBookDto) {
        if(addressBookDto == null){
            return false;
        }
        AddressBook addressBook = new AddressBook();
        BeanUtils.copyProperties(addressBookDto, addressBook);
        addressBook.setUserId(UserThreadLocalUtils.getId());
        int updateResult = addressBookMapper.updateAddressBook(addressBook);
        return updateResult == 1;
    }

    @Override
    public boolean removeAddressBooksByIds(List<String> idList) {
        if(idList == null || idList.size() == 0){
            return false;
        }
        int deleteResult = addressBookMapper.deleteAddressBooksByIds(idList);
        if(deleteResult != idList.size()){
            return false;
        }
        return true;
    }
}
